package cn.semyes.ylipfilter.controller.user;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import cn.semyes.ylipfilter.bean.Constants;
import cn.semyes.ylipfilter.common.util.GetCurDateTime;
import cn.semyes.ylipfilter.common.util.GetHttpJsonobject;



/**
 * 用户入口接口
 */
@RestController
public class UserController {
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	private String params = null;
	private JSONObject res = null;
	
	private GetCurDateTime getCurDateTime;
	private Calendar c;

	private GetHttpJsonobject getHttpJsonobject; 
  
    @GetMapping("/")
    public void getCode(@RequestParam("code") String code, HttpServletRequest request, HttpServletResponse response) {
    	
    	if (code != null) {
	    	getHttpJsonobject = new GetHttpJsonobject();
	    	//logger.info("用户授权百度返回的code:"+code);
	    	
	    	params = "client_id="+Constants.CLIENT_ID+"&client_secret="+Constants.CLIENT_SECRET+"&grant_type="+Constants.GRANT_TYPE+"&code="+code+"&redirect_uri="+Constants.REDIRECT_URI+"&state="+Constants.STATE;
	    	if (res == null) {
	    		res = getHttpJsonobject.httpsRequest(Constants.URL, "POST", params);				
			}
	    	
	    	//logger.info("后台请求百度返回的refresh_token和access_token:"+res);
			if (res != null) {
				c = Calendar.getInstance();	
				removeSession(request);
				
				getCurDateTime = new GetCurDateTime();
				request.getSession().setAttribute("cur_datatime", getCurDateTime.getCurTime());
				
				request.getSession().setAttribute("cur_username", res.getString("username"));
				request.getSession().setAttribute("cur_refreshToken", res.getString("refresh_token"));
				request.getSession().setAttribute("cur_accessToken", res.getString("access_token"));
				request.getSession().setAttribute("cur_user", res.getString("identifier"));
				
				//登录有效期30分钟
				if (request.getSession().getAttribute("loginTime") != null) {
					//logger.info("登录时间："+ (Long.parseLong(request.getSession().getAttribute("loginTime").toString()))/(1000*60));
					//logger.info("登录时间段："+ (c.getTimeInMillis() - Long.parseLong(request.getSession().getAttribute("loginTime").toString()))/(1000*60));
					if ((c.getTimeInMillis() - Long.parseLong(request.getSession().getAttribute("loginTime").toString()))/(1000*60) < 30) {
						try {
							res = null;
							response.sendRedirect("/home/index.do");
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}else {
						try {
							res = null;
							response.sendRedirect("/login/index.do");
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					
				} else {
					try {
						res = null;
						response.sendRedirect("/login/index.do");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
			
    	}
	
		
    }
    
    //每次进来先清除用户session
    private void removeSession(HttpServletRequest request){
    	HttpSession httpsession = request.getSession();
		httpsession.removeAttribute("cur_datatime");
		httpsession.removeAttribute("cur_username");
		httpsession.removeAttribute("cur_refreshToken");
		httpsession.removeAttribute("cur_accessToken");
		httpsession.removeAttribute("cur_user");
		httpsession.removeAttribute("cur_domain");
		httpsession.removeAttribute("cur_bd_userId");
		httpsession.removeAttribute("saveToKad");
    }
    
   

}
